<?php
namespace app\bis\controller;
use think\Controller;

class Login extends Controller
{
    public function index(){
    	if(request()->isPost()){
    		//处理相关逻辑
    		//获取数据
    		$data = input('post.');
    		//严格校验数据

            $account = model('BisAccount')->get(['username' => $data['username']]);
            if(!$account || $account->status != 1){
            	$this->error('用户不存在，或者未被审核通过');
            }
            if($account->password != md5($data['password'].$account->code)){
            	$this->error('用户名或密码错误');
            }
            //allowField过滤data中非数据表中的字段
            $update = model('BisAccount')->allowField(true)->save(['last_login_time' => time()],['id' => $account->id]);
            //保存用户信息，bis为作用域
            session('BisAccount',$account,'bis');

//    		return $this->success('登陆成功',url('index/index'));
            return $this->redirect(url('login/index'));
    	}

    	$session = session('BisAccount','','bis');
    	if($session && $session->id){
    		return $this->redirect(url('index/index'));
    	}
        return $this->fetch();
    }

    public function logout(){
    	session(null,'bis');
    	$this->redirect('login/index');
    }
}
